package com.jinyi.wwdeng.ui.steps.impl

import android.content.Context
import com.jinyi.base.mvp.BasePresenter
import com.jinyi.wwdeng.AppConfigs
import com.jinyi.wwdeng.ui.steps.contract.StepListContract
import org.json.JSONObject

/**
 * JinYiTest
 *
 * @Author：wwdeng
 * @CreateTime：2024/4/26 18:45
 * <pre>
 * Presenter 实现类
</pre> *
 */
class StepListPresenterImpl : BasePresenter<StepListContract.View?, StepListModelImpl?>(), StepListContract.Presenter {

    //流程数据
    private var mData: JSONObject? = null

    override fun createModel(): StepListModelImpl {
        return StepListModelImpl(this)
    }

    /**
     * 获取全部流程数据
     */
    fun loadStepDataListAction(){
        if (isViewDestory()) {
            return
        }
        model!!.getStepListData()
    }

    /**
     * 提交流程数据
     */
    fun onSubmitStepAction(params: JSONObject){
        if (isViewDestory()) {
            return
        }
        mvpView!!.showLoading("")
        model!!.onSubmitStepAction(params)
    }

    /**
     * 设置流程数据
     */
    override fun setStepListData(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        this.mData = data
        val allStepTitles = data.optJSONArray("allStepTitles")
        val tabList = arrayListOf<JSONObject>()
        //当前进行的步骤
        val curtIndex = getStepIndex()
        for (i in 0 until allStepTitles!!.length()) {
            val tabObj = JSONObject()
            tabObj.put("tabId", i)
            tabObj.put("tabTitle", allStepTitles.optString(i))
            tabObj.put("isSelect", i == curtIndex)
            tabList.add(tabObj)
        }
        //设置所有步骤列表
        mvpView!!.showStepTabList(tabList,curtIndex)
        onShowPageByStep()
    }

    /**
     * 提交流程成功
     *  需要更新总流程
     */
    override fun onSubmitStepSuccess(data: JSONObject) {
        if (isViewDestory()) {
            return
        }
        val code = data!!.optInt("code")
        when(code){
            0 -> {//成功
                mvpView!!.showStepSubmitSuccess()
            }
            else -> {//失败
                val msg = data.optString("msg")
                mvpView!!.showToast(msg)
            }
        }
    }

    /**
     * 根据Step类型显示不同的Page
     */
    private fun onShowPageByStep(){
        //当前进行的步骤
        val curtIndex = getStepIndex()
        val allSteps = mData!!.optJSONArray("allSteps")
        if (curtIndex == -1){//所有步骤已完成
            mvpView!!.showCreditApplyPage()
            return
        }
        //获取对应的key
        val curtKey = allSteps?.optString(curtIndex)
        //Contact 类型
        if (mData!!.has(curtKey)){
            val stepData = mData!!.optJSONObject(curtKey)
            stepData?.put("stepTitle",curtKey)
            if (stepData != null && stepData.has("isContact") && stepData.optString("isContact") == "YES"){
                mvpView!!.showContactInfoPage(stepData)
                return
            }
        }

        //todo Ocr证件照样式
        if (curtKey!!.contains("ocr_info")){
            mvpView!!.showOcrInfoPage()
            return
        }

        //todo Selfie自拍照样式
        if (curtKey.contains("sefie_info")){
            mvpView!!.showSelfieInfoPage()
            return
        }

        //todo Account账户样式（法语地区绑定账号）
        if (curtKey.contains("account_info") && AppConfigs.isFrench()){
            mvpView!!.showAccountInfoFrenchPage()
            return
        }
        //常规通用样式
        if (mData!!.has(curtKey)){
            val stepData = mData!!.optJSONObject(curtKey)
            stepData?.put("stepTitle",curtKey)
            mvpView!!.showCommonInfoPage(stepData)
        }
    }

    /**
     * 获取 steps 中第一个在步骤在总步骤中是第几个
     */
    private fun getStepIndex(): Int{
        var index = -1
        //剩余未完成步骤
        val steps = mData!!.optJSONArray("steps")
        if (steps == null || steps.length() == 0){//已完成
            return index
        }
        //总步骤
        val allSteps = mData!!.optJSONArray("allSteps") ?: return index

        //当前进行的步骤
        val curtStep = steps.optString(0)
        for (i in 0..<allSteps.length()){
            if (curtStep == allSteps.optString(i)){
                index = i
                break
            }
        }
        return index
    }

    override fun onRequestFinish() {
        super.onRequestFinish()
        if (isViewDestory()) {
            return
        }
        mvpView!!.hideLoading()
    }

    override val context: Context?
        get() = if (!isViewDestory()) {
            mvpView!!.getContext()
        } else null
}
